package com.cyh.Aggregation_Trigger_Task_Interruptted_Handle;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.log4j.Logger;

/**
 * Created by yanhuche on 3/15/2017.
 */
public class AggregationTrigger implements Runnable {
    private static final Logger LOGGER = Logger.getLogger(AggregationTrigger.class);


    protected Future<?> future;
    protected ExecutorService scheduler;
    protected Runnable executionTask;


    public AggregationTrigger() {
        scheduler = Executors.newSingleThreadExecutor();
        executionTask = new AggregationTask();
    }

    @Override
    public void run() {
        LOGGER.debug("future_2: " + future);
        if (future != null) {
            // cancel previous round report running
            boolean cancelled = future.cancel(true);
            LOGGER.debug("cancelled: " + cancelled);
        }

        future = scheduler.submit(executionTask);
        LOGGER.debug("future_1: " + future);
    }

}
